﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var keyValue = $.request("keyValue");

    $(function () {
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetDeptByUsersJson",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                var depts = [],
                    users = [],
                    index = 0;
                var F_OrganizeId = "";
                for (var j = 0; j < result.length; j++) {
                    var item = result[j];
                    if (j == 0) {
                        depts.push('<option value="' + item.F_OrganizeId + '">' + item.F_OrganizeName + '</option>');
                        F_OrganizeId = item.F_OrganizeId;
                    } else {
                        if (F_OrganizeId != item.F_OrganizeId) {
                            depts.push('<option value="' + item.F_OrganizeId + '">' + item.F_OrganizeName + '</option>');
                            F_OrganizeId = item.F_OrganizeId;
                        }
                    }
                    users.push('<option value="' + item.F_UserId + '">' + item.F_UserName + '</option>');
                }
                $("#depts").html(depts.join(''));
                $("#users").html(users.join(''));
            }
        });
    });

    function btn_add() {
        var option = $("#users option:selected");

        if (option.length == 1) {
            $("#F_UserId").html(option[0].outerHTML);
        } else if (option.length > 1) {
            $.modalAlert('最多只能选择一个用户！', 'warning')
        } else {
            $.modalAlert('请选择相关用户！', 'warning');
        }
    }

    function btn_del() {
        $("#F_UserId").html('');
    }

    function submitForm() {
        var keyValue = $.request("keyValue"),
            F_UserId = $("#F_UserId option").val(),
            Remark = $("#Remark").val();

        if (!F_UserId) {
            $.modalAlert('请选择你要让单的用户！', 'error');
            return;
        }
        
        $.submitForm({
            url: "/OrderFlowManage/ClientOrder/SubmitDeptByUsers?keyValue=" + keyValue,
            //param: $("#form1").formSerialize(),
            param: {
                F_UserId: F_UserId,
                Remark: Remark
            },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        });
    }

    function submitOrderForm() {
        var F_UserId = $("#F_UserId option").val(),
            Remark = $("#Remark").val();

        if (!F_UserId) {
            $.modalAlert('请选择你要指派的用户！', 'error');
            return;
        }
        var param = { F_UserId: F_UserId, Remark: Remark };
        //console.log(param);
        $.submitForm({
            url: "/OrderFlowManage/ClientOrder/SubmitOrderDeptByUsers?keyValue=" + keyValue,
            param: param,
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        });
    }

    function ChangeValue() {
        //alert($("#depts").val());
        var deptId = $("#depts").val();
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetDeptByUsersByDeptId?deptId=" + deptId +"",
            data: {},
            dataType: "json",
            async: false,
            success: function (data) {
                var depts = [],
                    users = [];                   
                for (var j = 0; j < data.length; j++) {
                    var item = data[j];                   
                    users.push('<option value="' + item.F_UserId + '">' + item.F_UserName + '</option>');
                }
                $("#users").html(users.join(''));
            }
        });
    }
</script>

<form id="form1">
    <div style="padding-top: 20px; margin-right: 20px;">
        <table class="form">
            <tr>
                <th class="formTitle">
                    <select id="depts" class="form-control" multiple style="height:200px;" onchange="ChangeValue()"></select>
                </th>
                <th class="formTitle">
                    <select id="users" class="form-control" multiple style="height:200px;"></select>
                </th>
                <th style="width: 60px; padding-left:30px;">
                    <div class="text-center">
                        <button type="button" class="btn btn-primary btn-xs" onclick="btn_add()">添加</button>
                        <br />
                        <br />
                        <button type="button" class="btn btn-primary btn-xs" onclick="btn_del()">删除</button>
                    </div>
                </th>
                <th class="formTitle">
                    <select id="F_UserId" name="F_UserId" class="form-control" multiple style="height:200px;"></select>
                </th>
            </tr>
            <tr>
                <th class="formTitle">备注：</th>
                <td class="formValue" colspan="3">
                    <textarea id="Remark" name="Remark" class="form-control" rows="4"></textarea>
                </td>
            </tr>
        </table>
    </div>
</form>


